package schemax

import (
	"gitee.com/zhongguo168a/gocodes/datax"
	"gitee.com/zhongguo168a/gocodes/datax/schemax/basickind"
	"gitee.com/zhongguo168a/gocodes/myx/errorx"
)

func InitObj(obj datax.M, decl *ClassDecl) (err error) {
	return initObj(obj, decl)

}

func initObj(obj datax.M, decl *ClassDecl) (err error) {
	for _, field := range decl.GetAllField() {
		fname := field.Alias()
		switch typ := field.Type.(type) {
		case *ClassType:
			if field.Value == "{}" {
				obj[fname] = map[string]interface{}{}
			} else {
				var m map[string]interface{}
				obj[fname] = m
			}
		case *ArrayType:
			//obj[fname] = []interface{}{}
		case *MapType:
			if field.Value == "{}" {
				obj[fname] = map[string]interface{}{}
			} else {
				var m map[string]interface{}
				obj[fname] = m
			}
		case *EnumType:
			iedecl := GetDeclByKey(typ.Decl)
			if iedecl == nil {
				return errorx.New("enum decl not found", datax.M{"fname": fname, "decl": typ.Decl})
			}
			edecl := iedecl.(*EnumDecl)
			obj[fname] = basickind.DefaultValue(basickind.Kind(edecl.Kind))
		case *BasicType:
			obj[fname] = basickind.DefaultValue(typ.Kind)
		}
	}
	return nil
}
